//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.Common.Validation
{
    using System;

    /// <summary>
    /// Wrapper class for simple validations.
    /// </summary>
    public static class CustomValidator
    {
        /// <summary>
        /// Checks whether the <paramref name="value"/> is null. If null throws <typeparamref name="ArgumentNullException"/>.
        /// </summary>
        /// <param name="value">Value to be checked.</param>
        /// <param name="parameterName">Name of the parameter.</param>
        public static void AssertNotNull(object value, string parameterName)
        {
            if (value == null)
            {
                throw new ArgumentNullException(parameterName);
            }
        }

        /// <summary>
        /// Checks whether the <paramref name="value"/> is null or empty string.  If null or empty string throws <typeparamref name="ArgumentNullException"/>.
        /// </summary>
        /// <param name="value">Value to be checked.</param>
        /// <param name="parameterName">Name of the parameter.</param>
        public static void AssertNotEmpty(string value, string parameterName)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException(parameterName);
            }
        }

        /// <summary>
        /// Executes the validation rule againts <paramref name="value"/>.  If the rule returns true then throws <typeparamref name="ArgumentException"/>.
        /// </summary>
        /// <typeparam name="T">Type of the <paramref name="value"/>.</typeparam>
        /// <param name="rule">Lamda function to be used for validation.</param>
        /// <param name="value">Value to be checked.</param>
        /// <param name="message">Message to be thrown.</param>
        public static void AssertRule<T>(Func<T, bool> rule, T value, string message)
        {
            if (rule(value))
            {
                throw new ArgumentException(message);
            }
        }
    }
}
